﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using StarsAdmin.Core.DB.Entities;

namespace StarsAdmin.Core.DB.Configs
{
    public class LoginLogConfig : IEntityTypeConfiguration<LoginLogEntity>
    {
        public void Configure(EntityTypeBuilder<LoginLogEntity> builder)
        {
            builder.ToTable("T_LoginLogs");
            // 一对多配置，并禁止级联删除
            builder.HasOne(e => e.User).WithMany().HasForeignKey(e => e.UserId).OnDelete(DeleteBehavior.Restrict);
            builder.Property(p => p.UserName).IsRequired().HasMaxLength(64);
            builder.Property(p => p.Ip).IsRequired().HasMaxLength(64);
            builder.Property(p => p.Browser).IsRequired(false).HasMaxLength(64);
            builder.Property(p => p.Os).IsRequired(false).HasMaxLength(64);
            builder.Property(p => p.Device).IsRequired(false).HasMaxLength(64);
            builder.Property(p => p.UserAgent).IsRequired().HasMaxLength(256);
            builder.Property(p => p.Message).IsRequired(false);
            builder.Property(p => p.Result).IsRequired(false);
            //日志不需要LastUpdateDateTime属性，所以设置忽略LastUpdateDateTime属性
            builder.Ignore(p => p.LastUpdateDateTime);
        }
    }
}